package view.mediator.weapon.bug_stick
{
	import model.weapon.WeaponModel;
	
	import org.robotlegs.mvcs.StarlingMediator;
	
	import service.SQLService;
	
	import view.component.weapon.bug_stick.BugStickListView;
	import view.event.weapon.BaseWeaponSimpleItemEvent;
	import view.event.weapon.bug_stick.BugStickListViewEvent;
	
	
	/**
	 * BugStickListView界面中介
	 * @author £iang
	 * 
	 */	
	public class BugStickListViewMediator extends StarlingMediator
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** BugStickListView界面 */
		[Inject]
		public var bugStickListView:BugStickListView;
		/** sql */
		[Inject]
		public var sqlService:SQLService;
		/** weaponModel */
		[Inject]
		public var weaponModel:WeaponModel;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 注册函数 
		 * 
		 */		
		override public function onRegister():void
		{
			bugStickListView.setListData( sqlService.getSimpleShortWeaponListInfo( "bug_stick_table" ) );
			addViewListener( BaseWeaponSimpleItemEvent.SEARCH_ROUTE, searchRouteHandler );
			addViewListener( BaseWeaponSimpleItemEvent.DETAIL, getDetailHandler );
			addContextListener( BugStickListViewEvent.SCROLL_ITEM, scrollItemHandler );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理请求查看升级路线后派发的事件 
		 * @param event 请求查看升级路线后派发的事件 
		 * 
		 */		
		protected function searchRouteHandler( event:BaseWeaponSimpleItemEvent ):void
		{
			var routeArr:Array = new Array();
			
			sqlService.getWeaponRoute( event.weaponId, "bug_stick_table", routeArr );
			bugStickListView.setWeaponRoute( bugStickListView.lastRouteArr, false );
			bugStickListView.setWeaponRoute( routeArr, true );
		}
		
		/**
		 * 处理请求获取详细信息后派发的事件 
		 * @param event 请求获取详细信息后派发的事件 
		 * 
		 */		
		protected function getDetailHandler( event:BaseWeaponSimpleItemEvent ):void
		{
			weaponModel.curSearchWeaponId = event.weaponId;
			dispatch( new BugStickListViewEvent( BugStickListViewEvent.GET_DETAIL ) );
		}
		
		/**
		 * 处理请求滚动条滚动至某一项后派发的事件 
		 * @param event 请求滚动条滚动至某一项后派发的事件 
		 * 
		 */	
		protected function scrollItemHandler( event:BugStickListViewEvent ):void
		{
			var showIndex:int = weaponModel.curSearchWeaponId - 1;
			
			bugStickListView.showItem( showIndex );
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 移除函数 
		 * 
		 */		
		override public function onRemove():void
		{
			removeViewListener( BaseWeaponSimpleItemEvent.SEARCH_ROUTE, searchRouteHandler );
			removeViewListener( BaseWeaponSimpleItemEvent.DETAIL, getDetailHandler );
			removeContextListener( BugStickListViewEvent.SCROLL_ITEM, scrollItemHandler );
			super.onRemove();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BugStickListViewMediator()
		{
			super();
		}
	}
}